package com.application.controller;


import com.application.dao.communityDao;
import com.application.domain.Activity;
import com.application.domain.User;
import com.application.domain.Community;
import com.application.domain.request;
import com.application.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/communitys")
@Api(value = "社团模块")
public class CommunityController {

    @Resource
    CommunityService communityService;
    @Resource
    UserService userService;
    @Resource
    ActivityService activityService;

    @Resource
    LeaderService leaderService;
    @Resource
    communityDao communityDao;

    @Resource
    RequestService requestService;

    @PostMapping
    @ApiOperation("保存数据")
    Result save(@RequestBody Community community){
        int save = communityService.saveCommunity(community);
        String mes = save!=0?"保存数据成功":"保存数据失败，请重新尝试";
        return new Result(save!=0?Code.SAVE_OK :Code.SAVE_ERR,save,mes);
    }

    @GetMapping("/{name}")
    @ApiOperation("根据社团名字获取信息")
    public Result getByName(@PathVariable String name){
        Community community = communityService.selectByName(name);
        String mes = community!=null?"查询成功":"查询失败，请重新尝试";
        return new Result(community!=null ? Code.SELECT_OK:Code.SELECT_ERR,community,mes);
    }

    @GetMapping
    @ApiOperation("获取全部社团信息")
    public Result getAll(){
        List<Community> communities = communityService.selectAll();
        String mes =communities!=null?"查询成功":"查询失败，请重新尝试";
        return new Result(communities!=null?Code.SELECT_OK:Code.SELECT_ERR,communities,mes);
    }

    @GetMapping("/getBySchool")
    @ApiOperation("根据校区来查询活动")
    public Result getBySchool(@RequestParam String school){
        List<Community> bySchool = communityService.getBySchool(school);

        String mes = bySchool !=null ? "查询成功":"查询失败";
        int code = bySchool !=null ? Code.SELECT_OK:Code.SELECT_ERR;
        return new Result(code,bySchool,mes);
    }

    @DeleteMapping("/deleteByName")
    @ApiOperation("根据id删除社团")
    public Result deleteById(@RequestParam String name,@RequestParam String openid){
        //  需要  负责人才可以进行删除
        User user = userService.selectById(openid);
        if(name.equals(user.getCommunity()) && user.getLevel() >=2){
        int deleteById = communityService.deleteByName(name);
        String mes = deleteById!=0?"删除成功":"删除失败，请重新尝试";
        return new Result(deleteById!=0?Code.DELETE_OK:Code.DELETE_ERR,deleteById,mes);
        }
        return new Result(Code.DELETE_ERR,null,"删除失败，权限不足");
    }

    @DeleteMapping
    @ApiOperation("删除全部社团")
    public Result deleteAll(){
        int deleteAll = communityService.deleteAll();
        String mes = deleteAll!=0?"删除成功":"删除失败，请重新尝试";
        return new Result(deleteAll!=0?Code.DELETE_OK:Code.DELETE_ERR,deleteAll,mes);
    }

    @DeleteMapping("/DeleteByList")
    @ApiOperation("根据多个id删除")
    public Result deleteByIds(@RequestParam List<String> names){
        int deleteByIds = communityService.deleteByNames(names);
        String mes = deleteByIds!=0?"删除成功":"删除失败，请重新尝试";
        return new Result(deleteByIds!=0?Code.DELETE_OK:Code.DELETE_ERR,deleteByIds,mes);
    }

    @PutMapping
    @ApiOperation("根据name进行更新")
    // 当想要一个  RequestBody的参数   而且加上一个另外的参数   可以使用放在请求头   不可以放在  RequestParam中
    public Result updateByName(@RequestBody Community community, @RequestHeader String openid){
        int updateByName= communityService.updateByName(community,openid);
        String mes = updateByName!=0?"修改成功":"修改失败，请重新尝试或查看权限";
        return new Result(updateByName!=0?Code.UPDATE_OK:Code.UPDATE_ERR,updateByName,mes);
    }


    @ApiOperation("图片确定存入数据库")
    @PostMapping("/download/commit")
    public Result DownLoadCommit(@RequestBody Community community){
        int downLoad = communityService.DownLoad(community);
        String mes = downLoad!=0?"保存成功":"保存失败，请重新尝试";
        Integer code = downLoad!=0?Code.SAVE_OK:Code.SAVE_ERR;

        return new Result(code,downLoad,mes);
    }

    @GetMapping("/member")
    @ApiOperation(value = "根据社团名字来查看社团的全部成员")
    public Result getAllMemberByName(@RequestParam String name){
        List<User> memberByName = userService.getMemberByName(name);

        String mes = memberByName!=null ? "查询成功":"查询失败，请重新尝试";
        int code = memberByName!=null ? Code.SELECT_OK:Code.SELECT_ERR;
        return  new Result(code,memberByName,mes);
    }

    @GetMapping("/activity")
    @ApiOperation(value = "根据社团的名字来查看开展的活动")
    public Result getAllActivityByName(@RequestParam String name){
        if(name!=null){
        List<Activity> allByCommunityName = activityService.getAllByCommunityName(name);
        String mes = allByCommunityName !=null ? "查询成功":"查询失败，请重新尝试";
        int code = allByCommunityName !=null ? Code.SELECT_OK:Code.SELECT_ERR;

        return  new Result(code,allByCommunityName,mes);
        }

        return new Result(Code.SELECT_ERR,null,"请输入对应的社团名字");
    }


    @PutMapping("/test")
    public Result test(@RequestBody Community Community){
//        LambdaQueryWrapper<community> lqw  = new LambdaQueryWrapper<>();
//        lqw.eq(community::getName,Community.getName());
//        int updateByName = communityDao.update(Community,lqw);
        //  共享锁无法使用
        int updateById = communityDao.updateById(Community);
        return new Result(1,updateById,null);
    }


    @PostMapping("/apply")
    @ApiOperation("申请加入社团")
    public Result applyForCommunity(@RequestParam String openid,
                                    @RequestParam String name
                                    ){
        User user = userService.selectById(openid);
        //   判断  是否已经进入了一个 社团
        if(user.getFlag()!=0){
            return new Result(Code.UPDATE_ERR,null,"已经进入了一个社团，请不要重复申请");
        }else{
                //  代表审核中
                user.setFlag(1);
                userService.updateById(user);
                //放入审核表格
                request request = new request(user.getUsername(),user.getTel(),openid,name);
                request.setName1(null);
                request.setFlag2(null);
                request.setId(null);
                int save = requestService.save(request);

                int code = save !=0 ? Code.UPDATE_OK : Code.UPDATE_ERR;
                String mes = save !=0 ?"已添加到申请表格中，请耐心等待":"申请失败，请重新尝试";
                return new Result(code,save,mes);
        }
    }

    @GetMapping("/CommunityChcek")
    @ApiOperation("查看社团申请表格")
    public Result CommunityCheck(@RequestParam String com_name,@RequestParam String openid){
        User user = userService.selectById(openid);
        //  就是需要是对应社团的对应的负责人
        if(user.getLevel()>=2 && user.getCommunity().equals(com_name)){
            List<request> requests = requestService.selectByCom_name(com_name);

            String mes = requests != null ? "查看成功":"没人申请加入社团";
            int code = requests !=null? Code.SELECT_OK:Code.SELECT_ERR;
            return  new Result(code,requests,mes);
        }else{
            return new Result(Code.SELECT_ERR,null,"权限不够或社团信息错误，请退出");
        }
    }
}
